package by.yativem.commons.task;

import java.util.Date;

import org.apache.log4j.Logger;

public abstract class AbstractTask implements Runnable {
	private static final Logger log = Logger.getLogger(AbstractTask.class);
	protected Date startDate;
	protected Date endDate;
	protected long taskId = -1;
	protected String taskName;
	protected String taskDescription;

	public abstract void doTask();

	public boolean isRunning() {
		return startDate != null && endDate == null;
	}

	public boolean isStarted() {
		return startDate != null;
	}
	
	public boolean isFinished(){
		return startDate != null && endDate != null;
	}
	
	

	public void run() {
		startDate = new Date(System.currentTimeMillis());
		try {
			doTask();
		} catch (Exception e) {
			log.error("Exception while running taks. Task stooped. ",e);
		} finally {
			endDate = new Date(System.currentTimeMillis());
		}
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public long getTaskId() {
		return taskId;
	}

	public void setTaskId(long taskId) {
		this.taskId = taskId;
	}

	public String getTaskName() {
		return taskName;
	}

	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}

	public String getTaskDescription() {
		return taskDescription;
	}

	public void setTaskDescription(String taskDescription) {
		this.taskDescription = taskDescription;
	}
	
	@Override 
	public String toString(){
		return new StringBuilder("Task id : ").append(this.taskId).append(" , task commandName :").append(this.taskName).toString();
	}
}
